package ttc.sftp.search;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class AsyncTaskGetFileName extends AsyncTask<ArrayList<String>, Integer, String> {

	@Override
	protected String doInBackground(ArrayList<String>... arg0) {
		// TODO Auto-generated method stub
		ArrayList<String> tempArrayList = arg0[0];
		/*String username = tempArrayList.get(0);
		String password = tempArrayList.get(1);
		String host = tempArrayList.get(2);
		String stringPort= tempArrayList.get(3);
		ChannelSftp sftpChannel;
		sftpChannel = createChannelsftp(username, password, host, stringPort);*/
		
		
		 File file[] = Environment.getExternalStorageDirectory().listFiles();
		 String localFileName =  recursiveFileFind(file);
		 Vector remoteFile;
		 /*String remoteFileName="";
		 try {
			remoteFile = sftpChannel.ls("/");
			remoteFileName= recursiveRemoteFile(remoteFile, sftpChannel);
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		 String allFileName = localFileName ;
		 saveFileOnsdcard(allFileName,"FileSearch.txt");
		
	
			
		
		return null;
	}
	
	
	
	//de quy cac file,thu muc tren sdcard
	  public String recursiveFileFind(File[] file1){
		    int i = 0;
		    String temp="";
		    String filePath="";
		    String fileName;
		        if(file1!=null){
		        while(i!=file1.length){
		            filePath = file1[i].getAbsolutePath();
		            fileName = file1[i].getName();
		            temp+=fileName+"-"+filePath+"\n";		            
		             if(file1[i].isDirectory()){
		                        File file[] = file1[i].listFiles();
		                        temp+=recursiveFileFind(file);
		             }
		             i++;
		            //Log.d(i+"", filePath);
		           
		        }
		      }
		        return temp;		         
		    }
	  
	
	  //de quy cac file thu muc tren server sftp
	  public String recursiveRemoteFile(Vector file1,ChannelSftp sftpChannel) throws SftpException{           
      	int i=0;
		    String temp="";
		    String filePath="";
		    String fileName="";
		        if(file1!=null){
		        while(i!=file1.size()){
		        	Object obj = file1.elementAt(i);	    		        	
		        	fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename();
		        	filePath = sftpChannel.pwd();
		            temp+=fileName+"-"+filePath+"\n";
		             if(((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getAttrs().isDir()){	    		            	 		
		                        sftpChannel.cd(filePath+File.separator+fileName);	
		                        Log.i("aa", filePath+File.separator+fileName);
		                        Vector file = sftpChannel.ls(filePath+File.separator+fileName);	
		                        String temp1="";
		                        temp1+=recursiveRemoteFile(file,sftpChannel);
		                        sftpChannel.cd(filePath);
		                        temp+=temp1;
		                       	    		                        	    		                      
		             }
		             ++i;	    		             
		        }	    		        	
		      }
		        return temp;         
	  }
	  
	  
	  //save file on sdcard
	  public void saveFileOnsdcard(String s,String FileName){
		  File file[] = Environment.getExternalStorageDirectory().listFiles();

		         try{
		         String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
		         
		         File fileDir = new File(baseDir+File.separator+"sftpandroid");
		         fileDir.mkdirs();
		         String name = FileName;
		         File filetemp = new File(baseDir+File.separator +fileDir.getName()+ File.separator+name);
		         filetemp.createNewFile();		
		         FileOutputStream fOut = new FileOutputStream(filetemp);
		         OutputStreamWriter writer = new OutputStreamWriter(fOut);
		         writer.append(s);			        
		         writer.close();
		         fOut.close();		         		         		         
		         }catch(IOException e){	        	
		         }		     
	  }
	  
	  //tao channel
	  public ChannelSftp createChannelsftp(String username,String password,String host,String stingPort){

			// TODO Auto-generated method stub

  		// TODO Auto-generated method stub
		  		int port = Integer.parseInt(stingPort);
				JSch jsch = new JSch();

				ChannelSftp sftpChannel=null;
				Session session = null;
  	        try {
  	            session = jsch.getSession(username,host, port);
  	            session.setConfig("StrictHostKeyChecking", "no");
  	            session.setPassword(password);
  	            session.connect(); 
  	            Channel channel = session.openChannel("sftp");
  	            channel.connect();
  	             sftpChannel = (ChannelSftp) channel;
  	        } catch (JSchException e) {
  	            e.printStackTrace(); 	           
  	        }
  	        return sftpChannel;
  	        	        
  	
	  }
	  
	  
	  
	  
	  
	  
	  
	  
}
